package com.yidu.boss.sys.controller;


import com.github.pagehelper.PageInfo;
import com.yidu.boss.sys.domain.User;
import com.yidu.boss.sys.service.impl.UserServiceImpl;
import com.yidu.boss.util.RandomString;
import com.yidu.boss.sys.domain.Log;
import com.yidu.boss.sys.service.impl.LogServiceImpl;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author luoyushan
 * @Date 2019/11/23 10:16
 */
@Controller
@RequestMapping("/log/")
public class LogController {
    @Resource
    private LogServiceImpl logService;
    @Resource
    private UserServiceImpl userService;

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Log> selectAll() throws ParseException {
        return logService.selectAll();
    }


    public boolean insertLog(Log log){
        return logService.insertSelective(log);
    }


    @RequestMapping("log")
    public String goLogJsp(Model model){
        List<User> users = userService.selectByExample(null);
        model.addAttribute("users",users);
        return "log";
    }

    @RequestMapping("getData")
    @ResponseBody
    public Map doSeeLog(Integer page, Integer limit) throws ParseException {
        PageInfo<Log> logPageInfo = logService.selectPage(page,limit);
        Map<String, Object> map = RandomString.pagingALG(logPageInfo, logPageInfo.getTotal());
        return map;
    }

    @RequestMapping("selectDate")
    @ResponseBody
    public Map selectRangeDate(String condition,Integer page,Integer limit,Log log) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if ("".equals(condition)){
            PageInfo<Log> logPageInfo = logService.selectPage(page,limit);
            Map<String, Object> map = RandomString.pagingALG(logPageInfo, logPageInfo.getTotal());
            return map;
        }
        Date value = simpleDateFormat.parse(condition.trim().substring(0, 11).trim());
        Date value1 = simpleDateFormat.parse(condition.trim().substring(12).trim());

        Calendar cl = Calendar.getInstance();
        if (value.getTime()>value1.getTime()) {
            cl.setTime(value);
            cl.add(Calendar.DATE,1);
            value=cl.getTime();
        }
        cl.setTime(value1);
        cl.add(Calendar.DATE,1);
        value1=cl.getTime();

        PageInfo<Log> logPageInfo = logService.selectRangeDate(value,value1,page,limit,log);
        Map<String, Object> map = RandomString.pagingALG(logPageInfo, logPageInfo.getTotal());
        return map;
    }





}
